/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.univcasa.managing.impl;

import com.univcasa.db.model.ComptePersonnel;
import com.univcasa.db.model.Criteres;
import com.univcasa.db.persist.HibernateUtil;
import com.univcasa.managing.ComptesPersManager;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


/**
 *
 * @author Work
 */
public class ComptesPersManagerImpl implements ComptesPersManager {

    private static Logger log = Logger.getLogger(ComptesPersManagerImpl.class);

    public boolean isExists(ComptePersonnel compte) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public ComptePersonnel getComptePersBySom(String som) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "From ComptePersonnel pers where pers.som =:som " ;
        Query query = session.createQuery( hql );
        //query.setString("cin", (String)map.get("cin") );
        query.setString("som", som );
        query.setMaxResults(1);
        return (ComptePersonnel) query.uniqueResult() ;
    }

     /*@TODO ajout d'autres criteres cin...dans la requete
     * apres resolution du like %
     *
     * */
    public ComptePersonnel getComptePersByCriteria(Criteres criteria) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "From ComptePersonnel pers where pers.som =:som and pers.cin=:cin " ;
        Query query = session.createQuery( hql );
        //query.setString("cin", (String)map.get("cin") );
        query.setString("som", criteria.getCritere("som") );
        query.setString("cin", criteria.getCritere("cin") );
        query.setMaxResults(1);
        ComptePersonnel pers = (ComptePersonnel) query.uniqueResult();
        return pers;
    }

    public List getAllComptesPers() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        String hql = "From ComptePersonnel" ;
        Query query = session.createQuery( hql );
        List lista = query.list();
        return lista;
    }

    public List getAllSomComptesPers() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        String hql = "Select pers.som From ComptePersonnel pers" ;
        Query query = session.createQuery( hql );
        List lista = query.list();

        return lista;
    }


    public void ajouterComptePers(ComptePersonnel pers) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.save(pers);
        tx.commit();
        log.info("SUCCESS ADD Personnel to DB " + pers );
    }


    public void ajouterComptesPers(List comptes) {
        Session session = HibernateUtil.getSessionFactory().openSession();

        for (Iterator itr = comptes.iterator(); itr.hasNext(); ){
            Transaction tx = session.beginTransaction();
            ComptePersonnel pers = (ComptePersonnel) itr.next();
            session.save(pers);
            tx.commit();
            log.info("SUCCESS ADD Personnel to DB " + pers );
        }
    }

    public void majComptePers(ComptePersonnel pers) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.merge(pers);
        tx.commit();
        log.info("SUCCESS MAJ Personnel to DB " + pers );
    }

    public void majComptesPers(List comptes) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        for (Iterator itr = comptes.iterator(); itr.hasNext(); ){
            ComptePersonnel pers = (ComptePersonnel) itr.next();
            Transaction tx = session.beginTransaction();
            session.merge(pers);
            tx.commit();
            log.info("SUCCESS MAJ Personnel to DB " + pers );
        }
    }
}